package com.example.coreshopapp.weight;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.example.coreshopapp.R;
import com.example.coreshopapp.base.BaseActivity;
import com.example.coreshopapp.keyboard.widget.CustomKeyboard;
import com.example.coreshopapp.utils.ArithUtils;

import java.util.ArrayList;
import java.util.Map;

/**
 * 信息提示框
 */
public class CustomAlertDialog {

    /**
     * 显示大的文本框
     */
    public static void showContentDialog(Context context, String title, String msg, final IAlertDialogListener listener) {
        final Dialog dialog = new Dialog(context, R.style.MyDialog);
        dialog.setContentView(R.layout.dialog_content);
        TextView tvTitle = (TextView) dialog.findViewById(R.id.tv_title);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setText(title);
        }
        ((TextView) dialog.findViewById(R.id.tv_message)).setText(msg);
        dialog.findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)  listener.onSure();
                dialog.dismiss();
            }
        });
        dialog.show();
        // 屏蔽home键
        boolean isLimitApp = true;
        if (isLimitApp){
            dialog.getWindow().setFlags(BaseActivity.FLAG_HOMEKEY_DISPATCHED, BaseActivity.FLAG_HOMEKEY_DISPATCHED);//屏蔽Home键
        }
    }

    /**
     * 显示提示框
     */
    public static void showMessageDialog(Context context, String title, String msg, final IAlertDialogListener listener) {
        final Dialog dialog = new Dialog(context, R.style.MyDialog);
        dialog.setContentView(R.layout.dialog_message);
        TextView tvTitle = (TextView) dialog.findViewById(R.id.tv_title);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setText(title);
        }
        ((TextView) dialog.findViewById(R.id.tv_message)).setText(msg);
        dialog.findViewById(R.id.btn_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)  listener.onSure();
                dialog.dismiss();
            }
        });
        dialog.show();
        boolean isLimitApp = true;
        if (isLimitApp){
            dialog.getWindow().setFlags(BaseActivity.FLAG_HOMEKEY_DISPATCHED, BaseActivity.FLAG_HOMEKEY_DISPATCHED);//屏蔽Home键
        }
    }

    /**
     * 显示提问框
     */
//    public static void showYesOrNoDialog(Context context, String title, String msg, final IAlertDialogYesOrNoListener listener) {
//        final Dialog dialog = new Dialog(context, R.style.MyDialog);
//        dialog.setContentView(R.layout.dialog_whether);
//        TextView tvTitle = (TextView) dialog.findViewById(R.id.tv_title);
//        if (TextUtils.isEmpty(title)) {
//            tvTitle.setVisibility(View.GONE);
//        } else {
//            tvTitle.setText(title);
//        }
//        ((TextView) dialog.findViewById(R.id.tv_message)).setText(msg);
//        dialog.findViewById(R.id.btn_left).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (listener != null)  listener.onSure(true);
//                dialog.dismiss();
//            }
//        });
//        dialog.findViewById(R.id.btn_right).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (listener != null)  listener.onSure(false);
//                dialog.dismiss();
//            }
//        });
//        dialog.show();
//        boolean isLimitApp = true;
//        if (isLimitApp){
//            dialog.getWindow().setFlags(BaseActivity.FLAG_HOMEKEY_DISPATCHED, BaseActivity.FLAG_HOMEKEY_DISPATCHED);//屏蔽Home键
//        }
//    }

    /**
     * 显示更新提醒框
     */
//    public static void showUpdataDialog(final Context context, String msg, final IAlertDialogListener listener) {
//        final Dialog dialog = new Dialog(context, R.style.MyDialog);
//        dialog.setContentView(R.layout.dialog_updata_apk);
//
//        ((TextView) dialog.findViewById(R.id.tv_message)).setText(msg);
//        dialog.findViewById(R.id.btn_left).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (listener != null)
//                    listener.onSure();
//                dialog.dismiss();
//            }
//        });
//        dialog.findViewById(R.id.btn_right).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
//        dialog.show();
//        boolean isLimitApp =  BaseApplication.getInstance().getSpUtil().getBoolean(Constants.spClass.SP_LIMIT_APP, false);
//        if (isLimitApp){
//            dialog.getWindow().setFlags(BaseActivity.FLAG_HOMEKEY_DISPATCHED, BaseActivity.FLAG_HOMEKEY_DISPATCHED);//屏蔽Home键
//        }
//    }

    /**
     * 数据输入框
     */
    public static void showNumericalDialog(final Context context,final Double mVales,final String mCaption, final IAlertDialogNumberListener listener) {
        final Dialog dialog = new Dialog(context, R.style.MyDialog);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        dialog.setContentView(R.layout.dialog_numerical);

        final CustomKeyboard numKeybord = (CustomKeyboard) dialog.findViewById(R.id.id_numkeyboard);
        final ArrayList<Map<String, String>> valueList = numKeybord.getValueList();
        final GridView gridView = numKeybord.getGridView();
        final EditText et_Number = (EditText) dialog.findViewById(R.id.id_txt_number);

        TextView tv_title = (TextView) dialog.findViewById(R.id.tv_title);
        tv_title.setText(mCaption);
        gridView.setOnItemClickListener((parent, view, position, id) -> {
            if (position < 11 && position != 9) {    //点击0~9按钮
                int mStar = 0;
                int mEnd = 0;
                if (et_Number.isFocused()) {
                    final int selStart = et_Number.getSelectionStart();
                    final int selEnd = et_Number.getSelectionEnd();
                    mStar = Math.max(0, Math.min(selStart, selEnd));
                    mEnd = Math.max(0, Math.max(selStart, selEnd));
                }
                final CharSequence selectedText = et_Number.getText().subSequence(mStar, mEnd);
                String selectStr = selectedText.toString();
                String content = et_Number.getText().toString().trim();
                if ((mStar != mEnd) && (selectStr != ""))
                {
                    String mLeftStr = "";
                    if (mStar > 0) mLeftStr = content.substring(0,mStar);
                    String mRightStr ="";
                    if (mEnd < content.length())  mRightStr = content.substring(mEnd,content.length());
                    content = mLeftStr + valueList.get(position).get("name") + mRightStr;
                    et_Number.setText(content);
                    et_Number.setSelection(mStar+1);
                }
                else {
                    int index = et_Number.getSelectionStart();
                    Editable editable = et_Number.getText();
                    editable.insert(index, valueList.get(position).get("name"));
                }
            } else {
                if (position == 9) {      //点击小数点
                    String content = et_Number.getText().toString().trim();
                    if (!content.contains(".")) {
                        int index = et_Number.getSelectionStart();
                        Editable editable = et_Number.getText();
                        editable.insert(index, valueList.get(position).get("name"));
                    }
                }

                if (position == 11) {      //点击退格键
                    String content = et_Number.getText().toString().trim();
                    if (content.length() > 0) {
                        int index = et_Number.getSelectionStart();
                        if (index > 0) {
                            Editable editable = et_Number.getText();
                            editable.delete(index - 1, index);
                        }
                    }
                }
            }
        });

        dialog.show();

        dialog.findViewById(R.id.btn_result_ok).setOnClickListener(v -> {
            String sNumber = et_Number.getText().toString();
            Double dNumber = ArithUtils.ConvertToDouble(sNumber);
            if (listener != null) listener.onSure(dNumber);
            dialog.dismiss();
        });

        dialog.findViewById(R.id.btn_cancel).setOnClickListener(v -> dialog.dismiss());
    }

    /**
     * 整单折扣输入框
     */
//    public static void showDiscountDialog(final Context context,final Double mAmount,final Double mDiscount,
//                                          final Double mAcAmount, final IAlertDialogDicListener listener) {
//        final Dialog dialog = new Dialog(context, R.style.MyDialog);
//        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
//                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
//        dialog.setContentView(R.layout.dialog_discountall);
//        final CustomKeyboard numKeybord = (CustomKeyboard) dialog.findViewById(R.id.id_numkeyboard);
//        final ArrayList<Map<String, String>> valueList = numKeybord.getValueList();
//        final GridView gridView = numKeybord.getGridView();
//        final TextView et_amount = (TextView) dialog.findViewById(R.id.id_txt_amount);
//        final EditText et_discount = (EditText) dialog.findViewById(R.id.id_txt_discount);
//        final EditText et_acamount = (EditText) dialog.findViewById(R.id.id_txt_acamount);
//        et_amount.setText(ArithUtils.ConvertToStrNotZero(mAmount));
//        et_discount.setText(ArithUtils.ConvertToStrNotZero(mDiscount));
//        et_acamount.setText(ArithUtils.ConvertToStrNotZero(mAcAmount));
//        Log.d("零售收银,", mAcAmount+"");
//        et_discount.setOnFocusChangeListener(new View.OnFocusChangeListener(){
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                if (hasFocus) {
//                    EditText at_activity = (EditText) dialog.findViewById(R.id.id_txt_activity);
//                    at_activity.setText("1");//得到焦点时，把值赋给他，以判断是否改变
//                }
//            }
//        });
//
//        et_acamount.setOnFocusChangeListener(new View.OnFocusChangeListener(){
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                if (hasFocus) {
//                    EditText at_activity = (EditText) dialog.findViewById(R.id.id_txt_activity);
//                    at_activity.setText("2");//得到焦点时，把值赋给他，以判断是否改变
//                }
//            }
//        });
//
//        et_discount.addTextChangedListener(new TextWatcher() {
//               @Override
//               public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//               }
//
//               @Override
//               public void onTextChanged(CharSequence s, int start, int before, int count) {
//               }
//
//               @Override
//               public void afterTextChanged(Editable s) {
//                   EditText at_activity_type = (EditText) dialog.findViewById(R.id.id_txt_activity);
//                   String sActivity = at_activity_type.getText().toString();
//                   int mActivity = ArithUtils.ConvertToInt(sActivity);
//                   if (mActivity == 1) {
//                       EditText mEt_AcAmount = (EditText) dialog.findViewById(R.id.id_txt_acamount);
//                       TextView mEt_Amount = (TextView) dialog.findViewById(R.id.id_txt_amount);
//                       Double dAmount = ArithUtils.ConvertToDouble(mEt_Amount.getText().toString());
//                       Double mDiscount = ArithUtils.div(ArithUtils.ConvertToDouble(s.toString()),100);
//                       Double mAcAmount = ArithUtils.round(ArithUtils.mul(dAmount,mDiscount),Constants.PointAmount);
//                       mEt_AcAmount.setText(mAcAmount.toString());
//                   }
//               }
//           });
//
//        et_acamount.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                EditText at_activity_type = (EditText) dialog.findViewById(R.id.id_txt_activity);
//                String sActivity = at_activity_type.getText().toString();
//                int mActivity = ArithUtils.ConvertToInt(sActivity);
//                if (mActivity == 2) {
//                    TextView mEt_Amount = (TextView) dialog.findViewById(R.id.id_txt_amount);
//                    EditText mEt_Discount = (EditText) dialog.findViewById(R.id.id_txt_discount);
//                    Double dAmount = ArithUtils.ConvertToDouble(mEt_Amount.getText().toString());
//                    Double mAcAmount = ArithUtils.ConvertToDouble(s.toString());
//                    Double mDiscount = ArithUtils.round(ArithUtils.div(ArithUtils.div(mAcAmount,dAmount),100),Constants.PointDiscount);
//                    mEt_Discount.setText(mDiscount.toString());
//                }
//            }
//        });
//
//        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//                EditText at_activity_type = (EditText) dialog.findViewById(R.id.id_txt_activity);
//                EditText at_activity = (EditText) dialog.findViewById(R.id.id_txt_discount);
//                String sActivity = at_activity_type.getText().toString();
//                int mActivity = ArithUtils.ConvertToInt(sActivity);
//                if (mActivity != 1) at_activity = (EditText) dialog.findViewById(R.id.id_txt_acamount);
//                if (position < 11 && position != 9) {    //点击0~9按钮
//                    int mStar = 0;
//                    int mEnd = 0;
//                    if (at_activity.isFocused()) {
//                        final int selStart = at_activity.getSelectionStart();
//                        final int selEnd = at_activity.getSelectionEnd();
//                        mStar = Math.max(0, Math.min(selStart, selEnd));
//                        mEnd = Math.max(0, Math.max(selStart, selEnd));
//                    }
//                    final CharSequence selectedText = at_activity.getText().subSequence(mStar, mEnd);
//                    String selectStr = selectedText.toString();
//                    String content = at_activity.getText().toString().trim();
//                    if ((mStar != mEnd) && (selectStr != ""))
//                    {
//                        String mLeftStr = "";
//                        if (mStar > 0) mLeftStr = content.substring(0,mStar);
//                        String mRightStr ="";
//                        if (mEnd < content.length())  mRightStr = content.substring(mEnd,content.length());
//                        content = mLeftStr + valueList.get(position).get("name") + mRightStr;
//                        at_activity.setText(content);
//                        at_activity.setSelection(mStar+1);
//                    }
//                    else {
//                        int index = at_activity.getSelectionStart();
//                        Editable editable = at_activity.getText();
//                        editable.insert(index, valueList.get(position).get("name"));
//                    }
//                } else {
//                    if (position == 9) {      //点击小数点
//                        String content = at_activity.getText().toString().trim();
//                        if (!content.contains(".")) {
//                            int index = at_activity.getSelectionStart();
//                            Editable editable = at_activity.getText();
//                            editable.insert(index, valueList.get(position).get("name"));
//                        }
//                    }
//
//                    if (position == 11) {      //点击退格键
//                        String content = at_activity.getText().toString().trim();
//                        if (content.length() > 0) {
//                            int index = at_activity.getSelectionStart();
//                            if (index > 0) {
//                                Editable editable = at_activity.getText();
//                                editable.delete(index - 1, index);
//                            }
//                        }
//                    }
//                }
//            }
//        });
//
//        dialog.show();
//
//        dialog.findViewById(R.id.btn_result_ok).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String sDiscount = et_discount.getText().toString();
//                Double dDiscount = ArithUtils.div(ArithUtils.ConvertToDouble(sDiscount),100);
//
//                String sAcAmount = et_acamount.getText().toString();
//                Double dAcAmount = ArithUtils.ConvertToDouble(sAcAmount);
//
//                if (listener != null) listener.onSure(dDiscount,dAcAmount);
//                dialog.dismiss();
//            }
//        });
//
//        dialog.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
//    }

    /**
     * 数据输入框
     */

    public interface IAlertDialogNumberListener {
        void onSure(double mValue);
    }

    public interface IAlertDialogCharListener {
        void onSure(String mValue);
    }

    public interface IAlertDialogDicListener {
        void onSure(double mDiscount,double mAcAmount);
    }

    public interface IAlertDialogListener {
        void onSure();
    }

    public interface IAlertDialogYesOrNoListener {
        void onSure(boolean bYesOrNo);
    }

}